package cn.tedu.community.commons.restful;

import cn.tedu.community.commons.excepion.ServiceException;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应结果
 */
@Data
public class JsonResult<T> implements Serializable {
    private Integer state;
    private String message;
    private T data;

    public static JsonResult<Void> ok() {
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data) {
        JsonResult jResult = new JsonResult();
        jResult.setState(ServiceCode.OK.getValue());
        jResult.setData(data);
        return jResult;
    }

    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getServiceCode(),e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode state, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(state.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
